package com.yanqu.road.server.cmd.activity.fivehero;

import com.yanqu.road.entity.activity.fivehero.FiveHeroConfig;
import com.yanqu.road.entity.activity.seasontask.SeasonTaskConditionData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.FiveHeroProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;

import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.fivehero.FiveHeroMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.math.BigInteger;

@Cmd(code = GameProtocol.S_FIVEHERO_CITY_SEARCH, desc = "")
public class FiveHeroSearchCityCmd extends AbstractCommand {

    private FiveHeroProto.FiveHeroCitySearchReqMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = FiveHeroProto.FiveHeroCitySearchReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        FiveHeroConfig config = FiveHeroMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        long num = 0;
        if(config.getLuoYangAttItem() == GameConfig.GAME_MONEY_INGOTS){
            AttributeModule atModule = player.getModule(AttributeModule.class);
            num = atModule.getAttribute(ePlayerAttrType.Ingots);
        }else{
            num = player.getModule(BagModule.class).getUserBagItemCount(config.getLuoYangAttItem());
        }
        FiveHeroProto.FiveHeroCitySearchReqMsg.Builder msg = reqMsg.toBuilder();
        msg.setItemNum(num);
        int groupId = GameUnionActivityGroupMgr.getGroupId(msg.getActivityId(),player.getUserId());
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_FIVE_HERO_SEARCH_CITY,
                msg,player.getUserId(),msg.getActivityId(),groupId);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }

        FiveHeroProto.FiveHeroCitySearchRespMsg.Builder respMsg = FiveHeroProto.FiveHeroCitySearchRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(ClientProtocol.U_FIVEHERO_CITY_SEARCH, respMsg);
    }
}
